package com.jxc.summer.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jxc.summer.dao.AccountMapper;
import com.jxc.summer.service.AccountService;

@Service
public class AccountServiceImpl implements AccountService {

	private static Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);
	
	@Autowired
	private AccountMapper accountMapper;
	
	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, readOnly=true)
	@Override
	public Map<String, Object> pageList(Map<String, Object> params) {
		List<Map<String, Object>> list = accountMapper.pageList(params);
		int count = accountMapper.pageListCount(params);
		// package result
		Map<String, Object> maps = new HashMap<String, Object>();
		maps.put("recordsTotal", count);		// 总记录数
		maps.put("recordsFiltered", count);	// 过滤后的总记录数
		maps.put("data", list); 
		return maps;
	}

	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED)
	@Override
	public int addAccount(Map<String, Object> params) {
		int result = accountMapper.addAccount(params);
		accountMapper.addAccountCompany(params);
		return result;
	}
	
	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED)
	@Override
	public int updateAccount(Map<String, Object> params) {
		int result = accountMapper.updateAccountState(params);
		accountMapper.updateAccountCompany(params);
		return result;
	}

	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED)
	@Override
	public int deleteAccount(Map<String, Object> params) {
		int result = accountMapper.deleteAccount(params);
		return result;
	}

	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, readOnly=true)
	@Override
	public boolean validateAccountNickname(Map<String, Object> params) {
		if(accountMapper.validateAccountNickname(params)==0){
			return true;
		}
		return false;
	}

	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, readOnly=true)
	@Override
	public boolean validateAccountUsername(Map<String, Object> params) {
		if(accountMapper.validateAccountUsername(params)==0){
			return true;
		}
		return false;
	}

	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, readOnly=true)
	@Override
	public boolean validateAccountPassword(Map<String, Object> params) {
		if(accountMapper.validateAccountPassword(params)==1){
			return true;
		}
		return false;
	}


}
